package com.shixu.framework.plugins.weixin.service;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import net.sf.json.JSONObject;

import com.shixu.framework.plugins.weixin.WeChat;
import com.shixu.framework.plugins.weixin.bean.WxCard;
import com.shixu.framework.plugins.weixin.bean.WxCardCash;
import com.shixu.framework.plugins.weixin.bean.WxCardDiscount;
import com.shixu.framework.plugins.weixin.bean.WxCardGeneralCoupon;
import com.shixu.framework.plugins.weixin.bean.WxCardGift;
import com.shixu.framework.plugins.weixin.bean.WxCardGroupon;
import com.shixu.framework.plugins.weixin.util.HttpKit;
import com.shixu.framework.plugins.weixin.util.WeixinConstant;


public class CardService {
	
	/**
	 * 创建卡券
	 * @param card 
	 * @return weixin response
	 */
	public String createWxCard(WxCard card,String accessToken){
		
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_CREATE + accessToken, card.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 创建折扣券
	 * @param card 
	 * @return weixin response
	 */
	public String createDiscountCard(WxCardDiscount card,String accessToken){
		
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_CREATE + accessToken, card.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 创建代金券
	 * @param card 
	 * @return weixin response
	 */
	public String createCashCard(WxCardCash card,String accessToken){
		
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_CREATE + accessToken, card.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 创建礼品券
	 * @param card 
	 * @return weixin response
	 */
	public String createGiftCard(WxCardGift card,String accessToken){
		
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_CREATE + accessToken, card.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		return response;
	}

	/**
	 * 创建团购券
	 * @param card 
	 * @return weixin response
	 */
	public String createCard(WxCardGroupon card,String accessToken){
		
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_CREATE + accessToken, card.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 创建优惠券
	 * @param card 
	 * @return weixin response
	 */
	public String createGeneralCard(WxCardGeneralCoupon card,String accessToken){
		
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_CREATE + accessToken, card.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 更新卡券信息
	 * @return weixin response
	 */
	public String updateCard(WxCard card,String accessToken){

		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_UPDATE + accessToken, card.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (ExecutionException e) {
			
			e.printStackTrace();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 卡券解密
	 * @param encryptCode
	 * @return
	 */
	public JSONObject decryptCode(String encryptCode,String accessToken){

		JSONObject param = new JSONObject();
		param.put("encrypt_code", encryptCode);
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CODE_DECRYPT + accessToken, param.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		return JSONObject.fromObject(response);
	}
	
	/**
	 * 卡券核销
	 * @param code 要消耗的序列号
	 * @param cardId 要消耗序列号所述的 card_id，创建卡券时use_custom_code 填写 true 时必填。非自定义code 不必填写。
	 * @return
	 */
	public JSONObject consumeCard(String code,String cardId,String accessToken){

		JSONObject param = new JSONObject();
		param.put("code", code);
		param.put("card_id", cardId);
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_CONSUME + accessToken, param.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		return JSONObject.fromObject(response);
	}
	
	/**
	 * 卡券code是否有效
	 * @param code 要消耗的序列号
	 * @param cardId 要消耗序列号所述的 card_id，创建卡券时use_custom_code 填写 true 时必填。非自定义code 不必填写。
	 * @return
	 */
	public JSONObject cardCodeIsEffective(String code,String cardId,String accessToken){

		JSONObject param = new JSONObject();
		param.put("code", code);
		param.put("card_id", cardId);
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_CODE_ISECFFECTIVE + accessToken, param.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		return JSONObject.fromObject(response);
	}
	
	/**
	 *  查询卡券详情
	 * @param cardId 卡券的card_id
	 * @return
	 */
	public JSONObject getWxCardInfo(String cardId,String accessToken){
		
		JSONObject param = new JSONObject();
		param.put("card_id", cardId);
		String response = null;
		try {
			response = HttpKit.post(WeixinConstant.CARD_DETAIL + accessToken, param.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		return JSONObject.fromObject(response);
	}
	
}
